package com.mall507.fivepeaks.widget.xdialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.util.Util;

/**
 * 系统强制退出APPDialog
 * */
public class XUpdateButtonDialog extends Dialog {

	private TextView content,confirm,cancel;
	private XUpdateButtonDialogCallBack callbackThis;
	private Context context;
	
	/**
	 * @param context
	 * @param msg 提示内容
	 * @param callback 选择结果回调
	 * */
	public XUpdateButtonDialog(Context context,String msg,XUpdateButtonDialogCallBack callback) {
		super(context, R.style.XDialogSubmit);
		this.context=context;
		callbackThis=callback;
		LayoutInflater in = LayoutInflater.from(context);
		View viewDialog = in.inflate(R.layout.base_xdialog_2button, null);
		content=(TextView) viewDialog.findViewById(R.id.content);
		confirm=(TextView) viewDialog.findViewById(R.id.confirm);
		cancel=(TextView) viewDialog.findViewById(R.id.cancel);
		confirm.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View view) {
				callbackThis.confirm();
			}
		});
		cancel.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View view) {
				callbackThis.cancel();
			}
		});
		content.setText(msg);
		setContentView(viewDialog);
		// 点击对话框外部取消对话框显示
		setCanceledOnTouchOutside(false);
	}
	
	/**
	 * @param context
	 * @param msg 提示内容
	 * @param confirmName 确定名称
	 * @param cancelName 取消名称
	 * @param callback 选择结果回调
	 * */
	public XUpdateButtonDialog(Context context,String msg,String confirmName,String cancelName,XUpdateButtonDialogCallBack callback) {
		super(context, R.style.XDialogSubmit);
		this.context=context;
		callbackThis=callback;
		LayoutInflater in = LayoutInflater.from(context);
		View viewDialog = in.inflate(R.layout.base_xdialog_2button, null);
		content=(TextView) viewDialog.findViewById(R.id.content);
		confirm=(TextView) viewDialog.findViewById(R.id.confirm);
		cancel=(TextView) viewDialog.findViewById(R.id.cancel);
		if(!Util.isEmpty(confirmName)){
			confirm.setText(confirmName);
		}
		if(!Util.isEmpty(cancelName)){
			cancel.setText(cancelName);
		}
		confirm.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View view) {
				callbackThis.confirm();
			}
		});
		cancel.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View view) {
				callbackThis.cancel();
			}
		});
		content.setText(msg);
		setContentView(viewDialog);
		// 点击对话框外部取消对话框显示
		setCanceledOnTouchOutside(false);
	}
	
	@Override
	public void show() {
		if(!isShowing()){
		   super.show();
		}
	}
	
	/**
	 * 选择结果回调
	 * */
	public interface XUpdateButtonDialogCallBack{
		  /**
		   * 确定
		   */
		  void confirm();
		  
		  /**
		   * 取消
		   */
		  void cancel();
	}
	
	
	private long exitTime;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			
			if ((System.currentTimeMillis() - exitTime) > 2000) // System.currentTimeMillis()无论何时调用，肯定大于2000
			{
				Toast toast=Toast.makeText(context, "再按一次退出程序", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				exitTime = System.currentTimeMillis();
			} else {
				callbackThis.cancel();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
